package com.sky.controller.user;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.sky.mapper.admin.ShopMapper;
import com.sky.pojo.Merchant;
import com.sky.pojo.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.admin.ShopService;
import io.swagger.v3.oas.annotations.links.Link;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController("userShopController")
@RequestMapping("/user")
@RequiredArgsConstructor
@Slf4j
public class ShopController {
    private static final String SHOP_STATUS_KEY="SHOP_STATUS";
    private final RedisTemplate redisTemplate;
    private final ShopService shopService;


    /*
    * 查询店铺的营业状态
    * 从上面设置的店铺营业状态中查询
    * */
    @GetMapping("/shop/status")
    public Result selectStatus(){
        Object statusObject = redisTemplate.opsForValue().get(SHOP_STATUS_KEY);
        String jsonStr = JSONUtil.toJsonStr(statusObject);
        int status = Integer.parseInt(jsonStr);
        log.info("查看当前店铺的营业状态：{}",status == 1 ? "营业中":"停业中");
        redisTemplate.opsForValue().set(SHOP_STATUS_KEY,1,30, TimeUnit.MINUTES);
        // redisTemplate.opsForValue().set(SHOP_STATUS_KEY,status);
        return Result.success(status);
    }
    /*
    * 根据查询商家信息
    * */
    @GetMapping("/shop/getMerchantInfo")
    public Result selectMerchantInfo(){
        Merchant merchant = shopService.selectMerchantInfo();
        return Result.success(merchant);
    }
    /*
    * 查看购物车中的菜品和套餐
    * */
    @GetMapping("/shoppingCart/list")
    public Result selectShoppingCart(){
        List<ShoppingCart> shoppingCartList = shopService.selectShoppingCart();
        return Result.success(shoppingCartList);
    }

    /*
    * 清空购物车
    * */
    @DeleteMapping("/shoppingCart/clean")
    public Result deleteShoppingCart(){
        shopService.deleteShoppingCart();
        return Result.success();
    }
}
